package cn.com.sinosoft.app.freemaker;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
    
public class HtmlGenerator {     

	public String generate(String template, Object object) throws IOException, TemplateException{
		BufferedWriter writer = null;
		String htmlContent;
		try{
			Configuration config = FreemarkerConfiguration.getConfiguration();
			Template tp = config.getTemplate(template);
			StringWriter stringWriter = new StringWriter();
			writer = new BufferedWriter(stringWriter);

			tp.setEncoding("UTF-8");
			tp.process(object, writer);
			htmlContent = stringWriter.toString();
			writer.flush();

		}finally{
			if(writer!=null)
				writer.close();
		}
		return htmlContent;
	}

} 